import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {adSystemApiUrl, tuikeSystemUrl} from '../../../../config/api';

@Injectable()

export class RoleService {

  constructor(
    private httpService: HttpService
  ) {
  }

  /**
   * 获取角色列表
   * @param name
   * @param belong
   * @param orderField
   * @param orderSort
   * @param pageSize
   * @returns {Observable<R>}
   */

  getRoleList(page, pageSize) {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/role_index?page=${page}&pageSize=${pageSize}`;
    // const url = `${ adSystemApiUrl }/v1/admin/roles/list?name=${name}&belong=${belong}&orderField=${orderField}&orderSort=${orderSort}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }


  getPermissionList(page, pageSize) {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/role_index?page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }

  /**
   * 获取角色详情
   * @param id
   * @returns {Observable<R>}
   */
  getRoleInfo(id) {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/role_info?id=${id}`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }

  /**
   * 角色添加
   * @param data
   * @returns {Observable<R>}
   */
  addRole(data) {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/role_add`;
    return this.httpService.httpPostAuth(url, data).map(data => data);
  }

  /**
   * 角色修改
   * @param data
   * @returns {Observable<R>}
   */
  editRole(data) {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/role_update`;
    return this.httpService.httpPostAuth(url, data).map(data => data);
  }

  /**
   * 角色删除
   * @param data
   * @returns {Observable<R>}
   */
  delRole(data) {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/role_delete`;
    return this.httpService.httpPostAuth(url, data).map(data => data);
  }

  /**
   * 权限列表
   * @returns {Observable<R>}
   */
  getPermissionGroup() {
    const url = `${ adSystemApiUrl }/v1/admin/permissions/group_list`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }


  getPermissionTree() {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/permission_tree`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }

  /**
   * 角色权限
   * @param id
   * @returns {Observable<R>}
   */
  getRolePermission(id) {
    const url = `${ adSystemApiUrl }/v1/admin/permission_role/${ id }/get`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }


  setRolePermission(permission_val, id) {
    const url = `${ adSystemApiUrl }/v1/admin/permission_role/set`;
    return this.httpService.httpPostAuth(url, {'role_id': id, 'permission': permission_val}).map(data => data);
  }

  getAllRole() {
    const url = `${tuikeSystemUrl}/api/v1/admin/rbac/all_roles`;
    return this.httpService.httpGetAuth(url).map(data => data);
  }
}
